package com.beemans.common.ui.views

import android.content.Context
import android.graphics.Color
import android.util.AttributeSet
import android.view.View
import android.widget.FrameLayout
import androidx.appcompat.widget.AppCompatImageView
import androidx.appcompat.widget.AppCompatTextView
import androidx.core.view.isVisible
import androidx.core.view.updateLayoutParams
import com.beemans.common.R
import com.beemans.common.databinding.ViewStripCardBinding
import com.beemans.common.ext.*
import com.tiamosu.databinding.delegate.toDataBinding

/**
 * @author tiamosu
 * @date 2021/3/15
 */
class StripCardView @JvmOverloads constructor(
    context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0
) : FrameLayout(context, attrs, defStyleAttr) {

    private val binding: ViewStripCardBinding by lazy { R.layout.view_strip_card.toDataBinding()!! }

    init {
        addView(binding.root)

        val ta = context.obtainStyledAttributes(attrs, R.styleable.StripCardView)

        //上划线
        binding.stripCardViewTopLine.also { lineTop ->
            val isVisible = ta.getBoolean(R.styleable.StripCardView_scv_lineTopVisible, false)
            lineTop.isVisible = isVisible

            val backgroundColor =
                ta.getColor(
                    R.styleable.StripCardView_scv_lineTopColor,
                    R.color.color_f2f2f2.toColor()
                )
            lineTop.setBackgroundColor(backgroundColor)

            val height =
                ta.getDimensionPixelSize(R.styleable.StripCardView_scv_lineTopHeight, 1.pt2px)
            lineTop.updateLayoutParams { this.height = height }

            val marginStart =
                ta.getDimensionPixelSize(R.styleable.StripCardView_scv_lineTopMarginStart, -1)
            if (marginStart != -1) {
                lineTop.updateMarginLayoutParams { this.marginStart = marginStart }
            }

            val marginEnd =
                ta.getDimensionPixelSize(R.styleable.StripCardView_scv_lineTopMarginEnd, -1)
            if (marginEnd != -1) {
                lineTop.updateMarginLayoutParams { this.marginEnd = marginEnd }
            }
        }

        //下划线
        binding.stripCardViewBotLine.also { lineBot ->
            val isVisible = ta.getBoolean(R.styleable.StripCardView_scv_lineBotVisible, false)
            lineBot.isVisible = isVisible

            val backgroundColor =
                ta.getColor(
                    R.styleable.StripCardView_scv_lineBotColor,
                    R.color.color_f2f2f2.toColor()
                )
            lineBot.setBackgroundColor(backgroundColor)

            val height =
                ta.getDimensionPixelSize(R.styleable.StripCardView_scv_lineBotHeight, 1.pt2px)
            lineBot.updateLayoutParams { this.height = height }

            val marginStart =
                ta.getDimensionPixelSize(R.styleable.StripCardView_scv_lineBotMarginStart, -1)
            if (marginStart != -1) {
                lineBot.updateMarginLayoutParams { this.marginStart = marginStart }
            }

            val marginEnd =
                ta.getDimensionPixelSize(R.styleable.StripCardView_scv_lineBotMarginEnd, -1)
            if (marginEnd != -1) {
                lineBot.updateMarginLayoutParams { this.marginEnd = marginEnd }
            }
        }

        //左边图标
        binding.stripCardIvLeft.also { ivLeft ->
            val isVisible = ta.getBoolean(R.styleable.StripCardView_scv_ivLeftVisible, true)
            val icon = ta.getResourceId(R.styleable.StripCardView_scv_ivLeftIcon, -1)
            ivLeft.isVisible = isVisible && icon != -1

            val imgWidth =
                ta.getDimensionPixelSize(R.styleable.StripCardView_scv_ivLeftWidth, 0)
            val imgHeight =
                ta.getDimensionPixelSize(R.styleable.StripCardView_scv_ivLeftHeight, 0)
            ivLeft.loadImage(icon) { options { override(imgWidth, imgHeight) } }

            if (ta.hasValue(R.styleable.StripCardView_scv_ivLeftColor)) {
                val color = ta.getColor(R.styleable.StripCardView_scv_ivLeftColor, Color.WHITE)
                ivLeft.setColorFilter(color)
            }

            val marginStart =
                ta.getDimensionPixelSize(R.styleable.StripCardView_scv_ivLeftMarginStart, -1)
            if (marginStart != -1) {
                ivLeft.updateMarginLayoutParams { this.marginStart = marginStart }
            }
        }

        //标题文字
        binding.stripCardTvTitle.also { tvTitle ->
            val isVisible = ta.getBoolean(R.styleable.StripCardView_scv_tvTitleVisible, true)
            tvTitle.isVisible = isVisible

            if (ta.hasValue(R.styleable.StripCardView_scv_tvTitle)) {
                val text = ta.getString(R.styleable.StripCardView_scv_tvTitle)
                tvTitle.text = text
            }

            if (ta.hasValue(R.styleable.StripCardView_scv_tvTitleColor)) {
                val textColor = ta.getColor(R.styleable.StripCardView_scv_tvTitleColor, Color.WHITE)
                tvTitle.setTextColor(textColor)
            }

            val textSize =
                ta.getDimensionPixelSize(R.styleable.StripCardView_scv_tvTitleSize, -1)
            if (textSize != -1) {
                tvTitle.updateTextSize(textSize.toFloat())
            }

            val isBold = ta.getBoolean(R.styleable.StripCardView_scv_tvTitleBold, false)
            tvTitle.updateTextBold(isBold)

            val marginStart =
                ta.getDimensionPixelSize(R.styleable.StripCardView_scv_tvTitleMarginStart, -1)
            if (marginStart != -1) {
                tvTitle.updateMarginLayoutParams { this.marginStart = marginStart }
            }
        }

        //描述图标
        binding.stripCardIvDesc.also { ivDesc ->
            val isVisible = ta.getBoolean(R.styleable.StripCardView_scv_ivDescVisible, true)
            val icon = ta.getResourceId(R.styleable.StripCardView_scv_ivDescIcon, -1)
            ivDesc.isVisible = isVisible && icon != -1

            val imgWidth =
                ta.getDimensionPixelSize(R.styleable.StripCardView_scv_ivDescWidth, 0)
            val imgHeight =
                ta.getDimensionPixelSize(R.styleable.StripCardView_scv_ivDescHeight, 0)
            ivDesc.loadImage(icon) { options { override(imgWidth, imgHeight) } }

            if (ta.hasValue(R.styleable.StripCardView_scv_ivDescColor)) {
                val color = ta.getColor(R.styleable.StripCardView_scv_ivDescColor, Color.WHITE)
                ivDesc.setColorFilter(color)
            }

            val marginEnd =
                ta.getDimensionPixelSize(R.styleable.StripCardView_scv_ivDescMarginEnd, -1)
            if (marginEnd != -1) {
                ivDesc.updateMarginLayoutParams { this.marginEnd = marginEnd }
            }
        }

        //描述文字
        binding.stripCardTvDesc.also { tvDesc ->
            val isVisible = ta.getBoolean(R.styleable.StripCardView_scv_tvDescVisible, true)
            tvDesc.isVisible = isVisible

            if (ta.hasValue(R.styleable.StripCardView_scv_tvDesc)) {
                val text = ta.getString(R.styleable.StripCardView_scv_tvDesc)
                tvDesc.text = text
            }

            if (ta.hasValue(R.styleable.StripCardView_scv_tvDescColor)) {
                val textColor = ta.getColor(R.styleable.StripCardView_scv_tvDescColor, Color.WHITE)
                tvDesc.setTextColor(textColor)
            }

            val textSize =
                ta.getDimensionPixelSize(R.styleable.StripCardView_scv_tvDescSize, -1)
            if (textSize != -1) {
                tvDesc.updateTextSize(textSize.toFloat())
            }

            val isBold = ta.getBoolean(R.styleable.StripCardView_scv_tvDescBold, false)
            tvDesc.updateTextBold(isBold)

            val marginEnd =
                ta.getDimensionPixelSize(R.styleable.StripCardView_scv_tvDescMarginEnd, -1)
            if (marginEnd != -1) {
                tvDesc.updateMarginLayoutParams { this.marginEnd = marginEnd }
            }
        }

        //小红点
        binding.stripCardIvDot.also { ivDot ->
            val isVisible = ta.getBoolean(R.styleable.StripCardView_scv_ivDotVisible, false)
            ivDot.isVisible = isVisible

            val icon = ta.getResourceId(R.styleable.StripCardView_scv_ivDot, -1)
            if (icon != -1) {
                val size = ta.getDimensionPixelSize(R.styleable.StripCardView_scv_ivDotSize, 0)
                ivDot.loadImage(icon) { options { override(size, size) } }
            }

            val marginEnd =
                ta.getDimensionPixelSize(R.styleable.StripCardView_scv_ivDotMarginEnd, -1)
            if (marginEnd != -1) {
                ivDot.updateMarginLayoutParams { this.marginEnd = marginEnd }
            }
        }

        //右边图标
        binding.stripCardIvRight.also { ivRight ->
            val isVisible = ta.getBoolean(R.styleable.StripCardView_scv_ivRightVisible, true)
            val icon = ta.getResourceId(R.styleable.StripCardView_scv_ivRightIcon, -1)
            ivRight.isVisible = isVisible && icon != -1

            val imgWidth =
                ta.getDimensionPixelSize(R.styleable.StripCardView_scv_ivRightWidth, 0)
            val imgHeight =
                ta.getDimensionPixelSize(R.styleable.StripCardView_scv_ivRightHeight, 0)
            ivRight.loadImage(icon) { options { override(imgWidth, imgHeight) } }

            if (ta.hasValue(R.styleable.StripCardView_scv_ivRightColor)) {
                val color = ta.getColor(R.styleable.StripCardView_scv_ivRightColor, Color.WHITE)
                ivRight.setColorFilter(color)
            }

            val marginEnd =
                ta.getDimensionPixelSize(R.styleable.StripCardView_scv_ivRightMarginEnd, -1)
            if (marginEnd != -1) {
                ivRight.updateMarginLayoutParams { this.marginEnd = marginEnd }
            }
        }

        ta.recycle()
    }

    fun setIvLeft(block: AppCompatImageView.() -> Unit) {
        binding.stripCardIvLeft.apply(block)
    }

    fun setTvTitle(block: AppCompatTextView.() -> Unit) {
        binding.stripCardTvTitle.apply(block)
    }

    fun setIvDesc(block: AppCompatImageView.() -> Unit) {
        binding.stripCardIvDesc.apply(block)
    }

    fun setTvDesc(block: AppCompatTextView.() -> Unit) {
        binding.stripCardTvDesc.apply(block)
    }

    fun setViewDesc(block: View.() -> Unit) {
        binding.stripCardViewDesc.apply(block)
    }

    fun setIvDot(block: AppCompatImageView.() -> Unit) {
        binding.stripCardIvDot.apply(block)
    }

    fun setIvRight(block: AppCompatImageView.() -> Unit) {
        binding.stripCardIvRight.apply(block)
    }
}